package com.xu.ai.model.multiplemodels.controller;

import com.xu.ai.model.multiplemodels.prompt.ChatClientPrompt;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.deepseek.DeepSeekChatOptions;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

/**
 * 多模型聊天客户端 controller
 *
 * @author xuguan
 * @since 2025/10/9
 */
@RestController
@RequestMapping("/api/multi-model/chat")
public class MultiModelController {
    private final ChatClient openAiChatClient;
    private final ChatClient deepSeekChatClient;
    private final ChatClient ollamaChatModel;

    public MultiModelController(OpenAiChatModel openAiChatModel,
                                DeepSeekChatModel deepSeekChatModel,
                                OllamaChatModel ollamaChatModel) {
        this.openAiChatClient = ChatClient.builder(openAiChatModel)
                .defaultSystem(ChatClientPrompt.DEFAULT_SYSTEM_PROMPT.render(Map.of("modelName", "openai")))
                .build();
        this.deepSeekChatClient = ChatClient.builder(deepSeekChatModel)
                .defaultSystem(ChatClientPrompt.DEFAULT_SYSTEM_PROMPT.render(Map.of("modelName", "deepseek")))
                .build();
        this.ollamaChatModel = ChatClient.builder(ollamaChatModel)
                .defaultSystem(ChatClientPrompt.DEFAULT_SYSTEM_PROMPT.render(Map.of("modelName", "ollama")))
                .build();
    }

    @GetMapping(value = "/openai", produces = "text/html;charset=UTF-8")
    public Flux<String> openaiChat() {
        return openAiChatClient.prompt()
                .user("你是什么系统")
                .options(DeepSeekChatOptions.builder().temperature(0.1).build())
                .stream()
                .content();
    }

    @GetMapping(value = "/deepseek", produces = "text/html;charset=UTF-8")
    public Flux<String> deepseekChat() {
        return deepSeekChatClient.prompt()
                .user("你是什么系统")
                .options(DeepSeekChatOptions.builder().temperature(0.1).build())
                .stream()
                .content();
    }

    @GetMapping(value = "/ollama", produces = "text/html;charset=UTF-8")
    public Flux<String> ollamaChat() {
        return ollamaChatModel.prompt()
                .user("你是什么系统")
                .options(DeepSeekChatOptions.builder().temperature(0.1).build())
                .stream()
                .content();
    }
}
